package com.example.mam.exception;

import com.example.mam.dto.BaseResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @author muncie [muncie.hoo@qq.com]
 */
@CrossOrigin
@RestControllerAdvice
public class GlobalExceptionHandler {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    /**
     * 未登录
     *
     * @param response
     * @return
     */
    @ExceptionHandler({UnauthenticatedException.class, AuthenticationException.class})
    public BaseResult<Object> authenticationException(HttpServletResponse response) {
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.NOT_LOGIN));
    }

    /**
     * 没有相应权限
     *
     * @return
     */
    @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
    public BaseResult<Object> authorizationException(HttpServletResponse response) {
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.NO_PERMISSIONS));
    }

    /**
     * 数据库唯一索引
     *
     * @param e
     * @return
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public BaseResult<Object> DuplicateKeyException(DuplicateKeyException e) {
        if (e.getMessage().contains("uk_mail")) {
            return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.EMAIL_REPEAT_ERROR));
        }
        if (e.getMessage().contains("uk_mobile")) {
            return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.MOBILE_REPEAT_ERROR));
        }
        if (e.getMessage().contains("uk_type_user_id")) {
            return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.APPLY_REPEAT));
        }
        if (e.getMessage().contains("username")) {
            return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.USERNAME_REPEAT_ERROR));
        }
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.REPEAT_ERROR));
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BizException.class)
    public BaseResult<Object> bizException(BizException e) {
        return new BaseResult<>(false, new BizExceptionMessage(e.getBizExceptionEnum()));
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UnsupportedEncodingException.class)
    public BaseResult<Object> unsupportedEncodingException(UnsupportedEncodingException e) {
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.ENCODING_ERROR));
    }

    /**
     * 请求方式错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public BaseResult<Object> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.METHOD_ERROR));
    }

    /**
     * 整体异常
     *
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    public BaseResult<Object> exception(HttpServletRequest request, Exception e) {
        // TODO
        LOG.error("uri: " + request.getRequestURI());
        e.printStackTrace();
        LOG.error(e.getMessage());
        return new BaseResult<>(false, new BizExceptionMessage(BizExceptionEnum.INNER_ERROR));
    }
}
